(in-package #:srfi.setagaya.mc)

(export '(case-87))

;; ================================================================
;; SRFI-87
(defun =>-finder (form)
  (dolist (x form nil)
    (and (find '=> x :test #'eq)
	 (return-from =>-finder t))))

(defmacro case-87 (&body form)
  (let ((clauses (cdr form)))
    (if (=>-finder clauses)
	(let ((eform)
	      (gs (gensym))
	      (key (car form)))
	  (dolist (clause clauses `(let ((,gs ,key)) (case ,key ,@(nreverse eform))))
	    (if (eq '=> (second clause))
		(destructuring-bind (key => receiver) clause
		  (declare (ignore =>))
		  (push `(,key ((lambda (x) (funcall ,receiver x)) ,gs)) eform))
		(push clause eform))))
	`(case ,@form))))



